package com.heima.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.constants.wemedia.WemediaConstants;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.*;
import java.util.Date;
import java.util.HashSet;

/**
 * 从本地文件批量添加敏感词
 */
@SpringBootTest
public class SensitiveBatch {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Test
    public void test() {
        HashSet<String> hashSet = new HashSet<>();


        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\DATA\\敏感词库\\政治类-反动词库.txt")));
            //批量读取敏感词文件添加到set中
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                hashSet.add(line);
            }
            // 循环向redis添加敏感词

            AdSensitive adSensitive = new AdSensitive();

            for (String s : hashSet) {
                redisTemplate.opsForSet().add(WemediaConstants.SENSITIVE, s);
                adSensitive.setSensitives(s);
                adSensitive.setCreatedTime(new Date());
                LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
                lambdaQuery.eq(AdSensitive::getSensitives, s);
                AdSensitive result = adSensitiveMapper.selectOne(lambdaQuery);
                if (result == null)
                    adSensitiveMapper.insert(adSensitive);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
